package com.cloud.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.admin.common.constant.UserConstants;
import com.cloud.admin.common.core.domain.AjaxResult;
import com.cloud.admin.common.core.page.TableDataInfo;
import com.cloud.admin.model.entity.SysDictType;
import com.cloud.admin.service.SysDicTypeService;
import com.cloud.admin.shiro.security.ShiroUtils;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * Created on 2019/7/25.
 *
 * @author douhaichao
 */
@RestController
@AllArgsConstructor
@RequestMapping("system/dict")
public class DictTypeController extends BaseController {

    private final String prefix = "system/dict/type/";

    private final SysDicTypeService dictTypeService;

    @RequiresPermissions("system:dict:view")
    @GetMapping()
    public ModelAndView dictType() {
        ModelAndView view = super.baseView("type", prefix + "type");
        return view;
    }

    @PostMapping("list")
    @RequiresPermissions("system:dict:list")
    public TableDataInfo list(SysDictType dictType, Page page) {
        IPage pageData = dictTypeService.selectPageByLike(page, dictType);
        return getDataTable(pageData);
    }

    /**
     * 新增字典类型
     */
    @GetMapping("add")
    public ModelAndView add() {
        ModelAndView view = super.baseView("add", prefix + "add");
        return view;
    }

    /**
     * 新增保存字典类型
     */
    @RequiresPermissions("system:dict:add")
    @PostMapping("add")
    public AjaxResult addSave(@Validated SysDictType dict) {
        if (UserConstants.DICT_TYPE_NOT_UNIQUE.equals(dictTypeService.selectOne(dict))) {
            return error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(ShiroUtils.getLoginName());
        dict.setCreateTime(new Date());
        return toAjax(dictTypeService.save(dict));
    }

    /**
     * 修改字典类型
     */
    @GetMapping("edit/{dictId}")
    public ModelAndView edit(@PathVariable("dictId") Long dictId) {
        ModelAndView view = super.baseView("edit", prefix + "edit");
        view.addObject("dict", dictTypeService.getById(dictId));
        ;
        return view;
    }

    /**
     * 修改保存字典类型
     */
    @RequiresPermissions("system:dict:edit")
    @PostMapping("edit")
    public AjaxResult editSave(@Validated SysDictType dict) {
        if (UserConstants.DICT_TYPE_NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(ShiroUtils.getLoginName());
        dict.setUpdateTime(new Date());
        return toAjax(dictTypeService.updateById(dict));
    }

    @RequiresPermissions("system:dict:remove")
    @PostMapping("remove")
    public AjaxResult remove(String ids) {
        try {
            return toAjax(dictTypeService.removeByIds(Arrays.stream(ids.split(",")).collect(Collectors.toList())));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 查询字典详细
     */
    @RequiresPermissions("system:dict:list")
    @GetMapping("detail/{dictId}")
    public ModelAndView detail(@PathVariable("dictId") Long dictId) {
        ModelAndView view = super.baseView("edit", "system/dict/data/data");
        view.addObject("dict", dictTypeService.getById(dictId));
        view.addObject("dictList", dictTypeService.selectAll(null));
        return view;
    }

    /**
     * 校验字典类型
     */
    @PostMapping("/checkDictTypeUnique")
    @ResponseBody
    public String checkDictTypeUnique(SysDictType dictType) {
        return dictTypeService.checkDictTypeUnique(dictType);
    }
}
